package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.mapper.EbRechargeActivityMapper;
import cn.com.chengmandian.bike.model.EbRechargeActivity;
import cn.com.chengmandian.bike.service.IEbRechargeActivityService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 电单车运营-充值送礼活动 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-02-28
 */
@Service
public class EbRechargeActivityServiceImpl extends ServiceImpl<EbRechargeActivityMapper, EbRechargeActivity> implements IEbRechargeActivityService {

    @Override
    public List<EbRechargeActivity> getList(Integer areaId) {
        Date now = new Date();
        return this.list(new LambdaQueryWrapper<EbRechargeActivity>().eq(EbRechargeActivity::getAreaId, areaId)
                .eq(EbRechargeActivity::getIsDeleted, CommonConstant.ZERO)
                .eq(EbRechargeActivity::getIsEnabled, CommonConstant.ONE)
                .le(EbRechargeActivity::getStartTime, now)
                .ge(EbRechargeActivity::getEndTime, now)
                .orderByDesc(EbRechargeActivity::getChargeMoney));
    }

    @Override
    public EbRechargeActivity getActivity(Integer areaId, BigDecimal money) {
        Date now = new Date();
        return this.getOne(new LambdaQueryWrapper<EbRechargeActivity>().eq(EbRechargeActivity::getAreaId, areaId)
                .eq(EbRechargeActivity::getIsDeleted, CommonConstant.ZERO)
                .eq(EbRechargeActivity::getIsEnabled, CommonConstant.ONE)
                .le(EbRechargeActivity::getStartTime, now)
                .ge(EbRechargeActivity::getEndTime, now)
                .le(EbRechargeActivity::getChargeMoney, money)
                .orderByDesc(EbRechargeActivity::getChargeMoney)
                .last("limit 1"));
    }
}
